<?php
namespace Home\Controller;
use Common\Controller\CommonController;

class ShopActivityTplsController extends CommonController {

    public function index()
    {
        $map = [];
        list($list, $page) = $this->lists('ShopActivityTpls', $map, 'sort desc,id asc');
        $this->assign('page',$page);
        $this->assign('list', $list);
        $this->display();
    }

    public function add()
    {
        $pid = I('pid/d', 0);
        $model = M('ShopActivityTpls');
        if(IS_POST){
            $data = I('post.');
            if(empty($data['img'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传模板图片'));
            }

            $id = $model->add($data);
            if($id){
                $this->ajaxReturn(array('s'=>true,'m'=>'添加成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'添加失败!'));
            }
        }else{
            $sort = $model->max('sort');
            $sort = $sort ? $sort+1 : 100;
            $this->assign('sort', $sort);
            $this->display();
        }
    }

    public function edit()
    {
        $id = I('id');
        $info = M('ShopActivityTpls')->where(['id'=>$id])->find();
        if(empty($info)){
            if(IS_POST){
                $this->ajaxReturn(array('s'=>false,'m'=>'信息不存在或已被删除!'));
            }
            $this->error('信息不存在或已被删除!');
        }
        if(IS_POST){
            $data = I('post.');
            if(empty($data['img'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传模板图片'));
            }

            $model = M('ShopActivityTpls');

            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function del(){
        
        $id = I('id');
        $res = M('ShopActivityTpls')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('index')));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }
    }
}